package com.example.myapplication.ContentTest;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.R;
import com.google.android.material.textfield.TextInputLayout;

public class DictionaryAddActivity extends AppCompatActivity {
  TextInputLayout wordLayout;
  TextInputLayout illustrationLayout;
  EditText wordText;
  EditText illustrationText;
  String updateWord;
  String updateIllustration;

  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dictionary_add_layout);
    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setTitle("添加信息");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    Init();

  }

  private void Init() {
    Button saveButton = (Button) findViewById(R.id.save_button);
    saveButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Save();
      }
    });
    wordLayout = findViewById(R.id.word_layout);
    illustrationLayout = findViewById(R.id.illustration_layout);
    wordText = findViewById(R.id.word_text);
    illustrationText = findViewById(R.id.illustration_text);
    updateWord = getIntent().getStringExtra("word");
    updateIllustration = getIntent().getStringExtra("illustration");
    wordText.setText(updateWord);
    illustrationText.setText(updateIllustration);

  }

  private void Save() {

    String wordStr = String.valueOf(wordText.getText());
    String illustrationStr = String.valueOf(illustrationText.getText());
    if (CheckData(wordStr, wordLayout) && CheckData(illustrationStr, illustrationLayout)) {

      if (updateWord != null && DataExist(updateWord)) {
        //说明此次操作是更新操作
        UpdateDictionary(wordStr, illustrationStr);

      } else {
        AddWord(wordStr, illustrationStr);
      }
    } else {
      Toast.makeText(this, "请按照提示输入数据", Toast.LENGTH_SHORT).show();
    }

  }

  private void AddWord(String word, String illustration) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(VocabularyProvider.COLUMN_WORD, word);
    contentValues.put(VocabularyProvider.COLUMN_ILLUSTRATION, illustration);

    Uri uri = getContentResolver().insert(VocabularyProvider.CONTENT_URI, contentValues);
    if (uri != null) {
      Toast.makeText(this, "创建生词成功", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(this,"创建生词失败",Toast.LENGTH_SHORT).show();
    }
  }

  private boolean CheckData(String string, TextInputLayout layout) {

    if (string.isEmpty()) {
      layout.setError("内容不能为空");
      return false;
    }
    layout.setError(null);
    return true;
  }

  private void UpdateDictionary(String newWord, String newIllustration) {
    ContentValues values = new ContentValues();
    values.put(VocabularyProvider.COLUMN_WORD, newWord);
    values.put(VocabularyProvider.COLUMN_ILLUSTRATION, newIllustration);
    int count = getContentResolver().update(VocabularyProvider.CONTENT_URI, values,
        VocabularyProvider.COLUMN_WORD + "=?", new String[] { updateWord });
    if (count > 0) {

      Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
    } else {
      Toast.makeText(this, "更新失败", Toast.LENGTH_SHORT).show();
    }
  }
  private boolean DataExist(String word) {
    ContentResolver contentResolver = getContentResolver();
    Uri uri = VocabularyProvider.CONTENT_URI;
    Cursor cursor =
        contentResolver.query(uri, new String[] { VocabularyProvider.COLUMN_WORD }, "word=?",
            new String[] { word }, null);
    if (cursor != null) {
      return true;
    }
    return false;
  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    setResult(1);

    finish();
  }
}
